package com.cgbl.service.assist

import android.util.Log
import com.alibaba.fastjson.JSON
import com.cgbl.service.GlApp
import com.netease.nimlib.sdk.team.model.CreateTeamResult
import com.netease.yunxin.kit.alog.ALog
import com.netease.yunxin.kit.corekit.im.utils.RouterConstant
import com.netease.yunxin.kit.corekit.model.ResultInfo
import com.netease.yunxin.kit.corekit.model.ResultObserver
import com.netease.yunxin.kit.corekit.route.XKitRouter
import com.netease.yunxin.kit.corekit.route.XKitRouter.withKey
import com.robot.base.model.TeamListBean

object ChatAssist {


    /**
     * 创建讨论组
     */
    fun createGroup(team: TeamListBean, createSuccess: () -> Unit) {
        val accountId = mutableListOf<String>()
        val nickName = mutableListOf<String>()
        team.accountDOS?.forEach {
            accountId.add(it.accountId ?: "")
            nickName.add(it.nickName ?: "")
        }
//        val list: ArrayList<String> = arrayListOf("robot", "robot1", "robot2", "robot3")
//        val list2: ArrayList<String> = arrayListOf("robot", "robot1", "wangfei", "wanfei1")
        Log.d("ChatAssist", "###### 创建群组     team  ${JSON.toJSONString(team)}")
        Log.d("ChatAssist", "###### 创建群组     accountIds  ${JSON.toJSONString(accountId)}")
        Log.d("ChatAssist", "###### 创建群组     nickName    ${JSON.toJSONString(nickName)}")

        withKey(RouterConstant.PATH_CREATE_NORMAL_TEAM_ACTION).withParam(
            RouterConstant.REQUEST_CONTACT_SELECTOR_KEY, accountId
        ).withParam(
            RouterConstant.KEY_REQUEST_SELECTOR_NAME, nickName
        ).navigate(object : ResultObserver<Any?> {
            override fun onResult(res: ResultInfo<Any?>) {
                if (res.success && res.value is CreateTeamResult) {
                    val teamInfo = (res.value as CreateTeamResult?)!!.team
                    withKey(RouterConstant.PATH_CHAT_TEAM_PAGE).withContext(GlApp.getContext()!!)
                        .withParam(RouterConstant.CHAT_KRY, teamInfo).navigate()
                    createSuccess.invoke()
                } else {
                    ALog.e("ChatAssist", "create team failed.")
                }
            }
        })
    }
}